package com.sinodata.bsm.center.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.DimensionDao;
import com.sinodata.bsm.common.vo.Dimension;
import com.sinodata.bsm.common.vo.DimensionType;

@Repository
public class DimensionDaoImpl extends BaseDao implements DimensionDao {

    @Override
    public Page<Dimension> findByPage(Page<Dimension> page, Condition condition) {
        // TODO Auto-generated method stub
        page.setTotal(getTotal(condition));
        StringBuilder hql = new StringBuilder(" from Dimension d where 1=1");
        hql.append(getConditonSql(condition));
        hql.append(getOrderSql(page));

        List<Dimension> list = getSession().createQuery(hql.toString()).setFirstResult(page.getFirstResultNo()).setMaxResults(page.getPageSize()).list();
        page.setRows(list);
        return page;
    }

    @Override
    public int getTotal(Condition condition) {
        // TODO Auto-generated method stub
        StringBuilder hql = new StringBuilder(" select count(*) from Dimension where 1=1 ");
        hql.append(getConditonSql(condition));
        Query query = this.getSession().createQuery(hql.toString());
        return ((Long) query.list().iterator().next()).intValue();
    }

    /**
     * 拼接查询条件
     * 
     * @param condition
     * @return
     */
    private StringBuilder getConditonSql(Condition condition) {
        StringBuilder hql = new StringBuilder();
        Map<String, String> condMap = condition.getWhere();
        if (condMap.size() != 0) {
            hql.append(" and ( ");
            Iterator<String> keyItr = condMap.keySet().iterator();
            while (keyItr.hasNext()) {
                String key = keyItr.next();
                String value = condMap.get(key).substring(1, condMap.get(key).lastIndexOf("'"));
                // 排除这两个特殊字符，对模糊查询有影响
                if (value.equals("'") || value.equals("%")) {
                    value = " ";
                }
                hql.append(key).append(" like '%" + value + "%'");
                hql.append(" and ");
            }
            hql.delete(hql.lastIndexOf(" and "), hql.length()).append(" ) ");
        }
        return hql;
    }

    /**
     * 获取排序sql
     * 
     * @param page
     * @return
     */
    private StringBuilder getOrderSql(Page<Dimension> page) {
        StringBuilder hql = new StringBuilder();
        Map<String, String> condMap = page.getOrder();
        if (condMap.size() != 0) {
            hql.append(" order by ");
            Iterator<String> keyItr = condMap.keySet().iterator();
            while (keyItr.hasNext()) {
                String key = keyItr.next();
                hql.append(key).append(" ").append(condMap.get(key));
                hql.append(" , ");
            }
            hql.delete(hql.lastIndexOf(" , "), hql.length());
        }
        return hql;
    }

    @Override
    public void batchDeleteByTypeId(Long[] typeId) {
        // TODO Auto-generated method stub
        this.getSession().createQuery("delete from Dimension where dimensionTypeId in (:dimensionTypeIds)").setParameterList("dimensionTypeIds", typeId).executeUpdate();
    }

    @Override
    public void batchDelete(Long[] ids) {
        // TODO Auto-generated method stub
        this.getSession().createQuery("delete from Dimension where id in(:ids)").setParameterList("ids", ids).executeUpdate();
    }

}
